package vista;

import java.awt.Image;
import java.net.URL;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import modulos.usuario.modelo.Cuenta_Usuario;

/**
 *
 * @author caesar
 */
public class jFrmLogin extends javax.swing.JDialog {

    Cuenta_Usuario eCuenta_usuario = new Cuenta_Usuario();
    protected List<Object[]> uloged = null;
    public static int a = 0, l = 0;    

    public jFrmLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();         
        poneImagenLogin();
    }

    public jFrmLogin(java.awt.Frame parent, boolean modal, frmPrincipal frmInicio) {
        super(parent, modal);
        initComponents();
        a = pnlLateral.getWidth();
        l = pnlLateral.getHeight();        
        poneImagenLogin();
        
    }

    public static void poneImagenLogin() {
        if (ClsInicio.perfilActual != null) {
            lblImagen.setIcon(new ImageIcon(ClsInicio.imagenLogin.getScaledInstance(240, 190, Image.SCALE_DEFAULT)));
        } else {
            Image im=new ImageIcon("src/jpgs/User-01.png").getImage().getScaledInstance(240, 190, Image.SCALE_DEFAULT);
            lblImagen.setIcon(new ImageIcon(im));
        }


    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        pnlBase = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        lblClave = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        txtClave = new javax.swing.JPasswordField();
        pnlLateral = new javax.swing.JPanel();
        lblImagen = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ingreso al sistema:");
        setName("jLogin"); // NOI18N
        setPreferredSize(new java.awt.Dimension(350, 170));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 150, 107, -1));

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 150, 106, -1));

        pnlBase.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder()));
        pnlBase.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblUsuario.setText("Usuario:");
        pnlBase.add(lblUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        lblClave.setText("Clave:");
        pnlBase.add(lblClave, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        txtUsuario.setBackground(new java.awt.Color(254, 237, 214));
        txtUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtUsuarioKeyReleased(evt);
            }
        });
        pnlBase.add(txtUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 213, -1));

        txtClave.setBackground(new java.awt.Color(254, 237, 214));
        txtClave.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtClaveKeyReleased(evt);
            }
        });
        pnlBase.add(txtClave, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 213, -1));

        getContentPane().add(pnlBase, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 10, 230, 130));

        pnlLateral.setBackground(new java.awt.Color(64, 65, 236));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/User-01.png"))); // NOI18N

        javax.swing.GroupLayout pnlLateralLayout = new javax.swing.GroupLayout(pnlLateral);
        pnlLateral.setLayout(pnlLateralLayout);
        pnlLateralLayout.setHorizontalGroup(
            pnlLateralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLateralLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43))
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        pnlLateralLayout.setVerticalGroup(
            pnlLateralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlLateralLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblImagen)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnlLateral, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 250, 180));

        getAccessibleContext().setAccessibleParent(this);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-517)/2, (screenSize.height-240)/2, 517, 240);
    }// </editor-fold>//GEN-END:initComponents

    private Cuenta_Usuario getCuentaUsuario() {
        Cuenta_Usuario cu = new Cuenta_Usuario();
        cu.setNomUsuario(txtUsuario.getText().trim());
        cu.setClave(txtClave.getText());
        return cu;
    }

    private void logearUsuario() {
        try {
            List<Object[]> lsUsuario = null;
            lsUsuario = eCuenta_usuario.loginUsuario(this.getCuentaUsuario());
            if (lsUsuario == null) {
                JOptionPane.showMessageDialog(null, "Nombre de usuario y/o clave es incorrecto ...", "Acceso denegado ...", JOptionPane.WARNING_MESSAGE);
                uloged = null;
                txtClave.requestFocus();
                txtClave.setSelectionStart(0);
                txtClave.setSelectionEnd(txtClave.getText().trim().length());
            } else {
                uloged = lsUsuario;//(Cuenta_Usuario)lsUsuario.get(0);
                txtUsuario.requestFocus();
                txtUsuario.setSelectionStart(0);
                txtUsuario.setSelectionEnd(txtUsuario.getText().trim().length());
                this.dispose();
                //JOptionPane.showMessageDialog(null, "Bienvenido, usted ha ingresado al sistema ...", "Ingreso", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            ex.getMessage();
        }
    }
    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        logearUsuario();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtUsuarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsuarioKeyReleased
        if (evt.getKeyCode() == 10) {
            txtClave.setSelectionStart(0);
            txtClave.setSelectionEnd(txtClave.getText().trim().length());
            txtClave.requestFocus();
        }
    }//GEN-LAST:event_txtUsuarioKeyReleased

    private void txtClaveKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClaveKeyReleased
        if (evt.getKeyCode() == 10) {
            logearUsuario();
        }
    }//GEN-LAST:event_txtClaveKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblClave;
    public static javax.swing.JLabel lblImagen;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPanel pnlBase;
    private javax.swing.JPanel pnlLateral;
    private javax.swing.JPasswordField txtClave;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
